define(function (require, exports) {
    // 当前日期
    var now = new Date();

    /**
     * 时间单位
     */
    exports.unit = {
        minute: 60 * 1000,
        hour: 60 * 60 * 1000,
        day: 24 * 60 * 60 * 1000,
        week: 7 * 24 * 60 * 60 * 1000
    };

    var getDateProperty = function (date) {
        // 日
        var day = date.getDate();
        // 月
        var month = date.getMonth();
        // 年
        var year = date.getYear();
        year += (year < 2000) ? 1900 : 0;
        // 当前日期为本周的第几天，周一为0，周日为6
        var dayOfWeek = date.getDay() - 1;
        dayOfWeek = dayOfWeek < 0 ? 6 : dayOfWeek;

        return {
            day: day,
            month: month,
            year: year,
            dayOfWeek: dayOfWeek
        }
    };

    /**
     * 根据指定天数创建日期
     * @param date 原日期对象
     * @param days 指定天数
     */
    var addDays = function (date, days) {
        date.setDate(date.getDate() + days);
        return date;
    };

    exports.addDays = addDays;

    exports.yesterday = function () {
        return exports.getDateBefore(new Date(), 1);
    };

    exports.tomorrow = function () {
        return exports.getDateAfter(new Date(), 1);
    };

    exports.getDateBefore = function (date, days) {
        return addDays(date, -days);
    };

    exports.getDateAfter = function (date, days) {
        return addDays(date, days);
    };

    //获得某月的天数
    exports.getMonthDays = function (month, date) {
        date = date || now;
        var monthStartDate = new Date(getDateProperty(date).year, month, 1);
        var monthEndDate = new Date(getDateProperty(date).year, month + 1, 1);
        return (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
    };

    //获得本季度的开始月份    
    exports.getQuarterStartMonth = function (date) {
        date = date || now;

        var quarterStartMonth = 0;
        var month = getDateProperty(date).month;

        if (month < 3) {
            quarterStartMonth = 0;
        } else if (2 < month && month < 6) {
            quarterStartMonth = 3;
        } else if (5 < month && month < 9) {
            quarterStartMonth = 6;
        } else if (month > 8) {
            quarterStartMonth = 9;
        }

        return quarterStartMonth;
    };

    //获得本周的开始日期
    exports.getWeekStartDate = function (date) {
        date = date || now;
        return new Date(
            getDateProperty(date).year,
            getDateProperty(date).month,
            getDateProperty(date).day - getDateProperty(date).dayOfWeek);
    };

    exports.getWeekStartString = function (date) {
        return exports.format(exports.getWeekStartDate(date));
    };

    //获得本周的结束日期
    exports.getWeekEndDate = function (date) {
        date = date || now;
        return new Date(
            getDateProperty(date).year,
            getDateProperty(date).month,
            getDateProperty(date).day - getDateProperty(date).dayOfWeek + 6);
    };

    exports.getWeekEndString = function (date) {
        return exports.format(exports.getWeekEndDate(date));
    };

    //获得本月的开始日期
    exports.getMonthStartDate = function (date) {
        date = date || now;
        return new Date(getDateProperty(date).year, getDateProperty(date).month, 1);
    };

    //获得本月的开始日期
    exports.getMonthStartString = function (date) {
        return exports.format(exports.getMonthStartDate(date));
    };

    //获得本月的结束日期
    exports.getMonthEndDate = function (date) {
        date = date || now;
        return new Date(getDateProperty(date).year,
            getDateProperty(date).month,
            exports.getMonthDays(getDateProperty(date).month));
    };

    //获得本月的结束日期
    exports.getMonthEndString = function (date) {
        return exports.format(exports.getMonthEndDate(date));
    };

    //获得本季度的开始日期
    exports.getQuarterStartDate = function (date) {
        date = date || now;
        return new Date(getDateProperty(date).year, exports.getQuarterStartMonth(), 1);
    };

    //获得本季度的开始日期
    exports.getQuarterStartString = function (date) {
        return exports.format(exports.getQuarterStartDate(date));
    };

    //获得本季度的结束日期
    exports.getQuarterEndDate = function (date) {
        date = date || now;
        var quarterEndMonth = exports.getQuarterStartMonth(date) + 2;
        return new Date(getDateProperty(date).year, quarterEndMonth, exports.getMonthDays(quarterEndMonth));
    };

    //获得本季度的结束日期
    exports.getQuarterEndString = function (date) {
        return exports.format(exports.getQuarterEndDate(date));
    };

    //获得本年的开始日期
    exports.getYearStartDate = function (date) {
        date = date || now;
        return new Date(getDateProperty(date).year, 0, 1);
    };

    //获得本年的开始日期
    exports.getYearStartString = function (date) {
        return exports.format(exports.getYearStartDate(date));
    };

    //获得本年的结束日期
    exports.getYearEndDate = function (date) {
        date = date || now;
        return new Date(getDateProperty(date).year, 11, 31);
    };

    //获得本年的结束日期
    exports.getYearEndString = function (date) {
        return exports.format(exports.getYearEndDate(date));
    };

    /**
     * 格式化日期
     * @param date
     * @param formatter 默认格式为yyyy-MM-dd
     * @returns 格式化后的日期字符串
     */
    exports.format = function (date, formatter) {
        formatter = formatter || "yyyy-MM-dd";
        var o = {
            "M+": date.getMonth() + 1, //月份
            "d+": date.getDate(), //日
            "h+": date.getHours() % 12 == 0 ? 12 : date.getHours() % 12, //小时
            "H+": date.getHours(), //小时
            "m+": date.getMinutes(), //分
            "s+": date.getSeconds(), //秒
            "q+": Math.floor((date.getMonth() + 3) / 3), //季度
            "S": date.getMilliseconds() //毫秒
        };
        var week = {
            "0": "\u65e5",
            "1": "\u4e00",
            "2": "\u4e8c",
            "3": "\u4e09",
            "4": "\u56db",
            "5": "\u4e94",
            "6": "\u516d"
        };
        if (/(y+)/.test(formatter)) {
            formatter = formatter.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
        }
        if (/(E+)/.test(formatter)) {
            formatter = formatter.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "\u661f\u671f" : "\u5468") : "") + week[date.getDay() + ""]);
        }
        for (var k in o) {
            if (new RegExp("(" + k + ")").test(formatter)) {
                formatter = formatter.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            }
        }
        return formatter;
    };

    /**
     * 从字符串生成日期对象
     * @param dateString 日期字符串
     * @param formatter 日期字符串格式，默认为yyyy-MM-dd hh:mm:ss
     * @returns 日期对象
     */
    exports.parse = function (dateString, formatter) {
        formatter = formatter || "yyyy-MM-dd hh:mm:ss";

        var o = {
            'y+': 'y',
            'M+': 'M',
            'd+': 'd',
            'h+': 'h',
            'm+': 'm',
            's+': 's'
        };

        var result = {
            'y': '',
            'M': '',
            'd': '',
            'h': '00',
            'm': '00',
            's': '00'
        };

        for (var k in o) {
            if (new RegExp('(' + k + ')').test(formatter)) {
                result[o[k]] = dateString.substring(
                    formatter.indexOf(RegExp.$1),
                    formatter.indexOf(RegExp.$1) + RegExp.$1.length);
            }
        }

        return new Date(result['y'], result['M'] - 1, result['d'], result['h'], result['m'], result['s']);
    };

    exports.setDate = function (input, value) {
        $(input).val(value).omCalendar('setDate', exports.parse(value));
    };
});